<!-- 添加员工弹出层 -->
<template>
  <el-dialog
    title="新增员工"
    :visible="showDialog"
    @close="closeDialog"
    @click.native="handleDialogClick"
  >
    <!-- 表单 -->
    <el-form ref="addForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" /></el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          :value="formData.departmentName"
          style="width:50%"
          placeholder="请选择部门"
          @click.native.stop="handleFocus"
        />
        <div v-if="isshow" class="tree-box">
          <el-tree
            v-loading="loading"
            :data="treeData"
            :props="{label :'name'}"
            @node-click="handleNodeClick"
          />
        </div>

      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item></el-form>
    <!-- footer插槽 -->
    <template #footer style="text-align: center;">
      <div style="text-align: center">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click.native="submitAdd">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
// 导入列举模块
import employees from '@/constant/employees'
import { reqAddEmployee } from '@/api/employees'

export default {
  props: {
    //   1父组件控制显示和隐藏
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      },
      treeData: [],
      isshow: false,
      loading: false,
      hireType: employees.hireType

    }
  },

  methods: {
    closeDialog() {
      // 对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.$emit('update:showDialog', false)
      this.$refs.addForm.resetFields() // 重置校验结果
    },
    async handleFocus() {
      // 开启和关闭弹窗
      this.isshow = !this.isshow
      //   开启加载
      //   做一个判断，如果组件是隐藏的话就不要执行以下代码，避免浪费性能
      if (!this.isshow) return
      this.loading = true

      console.log('点击')
      const { depts } = await getDepartments()
      console.log(depts)
      //   转换为树行组件
      const res = tranListToTreeData(depts, '')
      this.treeData = res
      //   关闭加载
      this.loading = false
      console.log(this.treeData)
    },
    // 弹窗组建的冒泡操作，目的是是点击弹窗组建的任何地方都可以关闭弹窗
    handleDialogClick() {
      // 关闭弹窗
      this.isshow = false
      //   这边一个小bug'，因为这个是靠点击部门冒泡的操作，不给它关闭冒泡的话它会上传到父组件，然后就会关闭弹窗
    },
    handleNodeClick(data) {
      console.log(data)
      // 判断是否是还可以打开的节点，是的话就不做任何操作，不是的话就把节点名称赋值给部门选项
      if (data.children && data.children.length > 0) return
      //   赋值
      this.formData.departmentName = data.name
      // 关闭弹窗
      this.isshow = false
    },
    // 新增员工按钮
    submitAdd() {
      // 1整体校验
      this.$refs.addForm.validate(async isok => {
        if (!isok) return
        // 调用添加接口
        await reqAddEmployee(this.formData)
        // 提示`信息
        this.$message.success('添加成功')
        // 关闭弹窗
        await this.closeDialog()
        // 子组件通知父组件更新数据
        this.$emit('addfrom')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-box {
  position: absolute;
  width: 50%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  overflow: auto;
  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
  }
}
</style>

